
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author khang
 */
public class Boss {
    private Sprite boss;
    private Sprite[] Bullet;
    private int[] isShoot,bulletMoveTime;
    private int iWidth,iHeight,iX,iY,timefire;
    private int health,nBullet,timewait;
    public Boss(Sprite boss,LayerManager lm,int width,int height){
        this.boss=boss;
        nBullet=15;
        Bullet=new Sprite[nBullet];
        isShoot=new int[nBullet];
        bulletMoveTime=new int[nBullet];
        iWidth=width;
        iHeight=height;
        timefire=0;
        health=1000;
        timewait=0;
        for(int i=0;i<nBullet;i++){
            try {
                Sprite bullet=new Sprite(Image.createImage("/so0.png"));
                Bullet[i]=bullet;
                Bullet[i].defineReferencePixel(Bullet[i].getWidth()/2, Bullet[i].getHeight()/2);
                Bullet[i].setVisible(false);
                lm.append(Bullet[i]);
                isShoot[i]=0;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        iX=boss.getRefPixelX();
        iY=boss.getRefPixelY();
    }
    public void moveBoss(){
        Random ran=new Random();
        int ranXY=ran.nextInt(4);
        switch(ranXY){
            case 0:{
                if(boss.getRefPixelX()-1>50){
                    boss.move(-1, 0);
                }
            }break;
            case 1:{
                if(boss.getRefPixelX()+1<iWidth-50){
                    boss.move(1, 0);
                }
            }break;
            case 2:{
                if(boss.getRefPixelY()-1>10){
                    boss.move(0, -1);
                }
            }break;
            case 3:{
                if(boss.getRefPixelY()+1<iHeight/3){
                    boss.move(0, 1);
                }
            }
        }
        iX=boss.getRefPixelX();
        iY=boss.getRefPixelY();
    }
    public void bossFire(Sprite player,int scrY){
        timefire--;
        for(int i=0;i<nBullet;i++){
            if(Bullet[i].getRefPixelY()>iHeight){
                Bullet[i].setVisible(false);
                isShoot[i]=0;
            }
        }
        if(health>700||(health<=500&&health>300)){
            for(int i=0;i<nBullet;i++){
                if(isShoot[i]!=0){
                    switch(isShoot[i]){
                        case 1:Bullet[i].move(0, 2);break;
                        case 2:Bullet[i].move(-1, 2);break;
                        case 3:Bullet[i].move(1, 2);break;
                    }
                }
            }
        }
        else if((health<=700&&health>500)||health<=300){
            for(int i=0;i<nBullet;i++){
                if(isShoot[i]!=0){
                    if(bulletMoveTime[i]>0){
                        switch(isShoot[i]){
                            case 1:Bullet[i].move(0, 2);break;
                            case 2:Bullet[i].move(-3, 2);break;
                            case 3:Bullet[i].move(3, 2);break;
                        }
                    }
                    else{
                        if(Bullet[i].getRefPixelX()==player.getRefPixelX()){
                            Bullet[i].move(2, 2);
                        }
                        if(Bullet[i].getRefPixelX()<player.getRefPixelX()){
                            Bullet[i].move(2, 2);
                        }
                        if(Bullet[i].getRefPixelX()>player.getRefPixelX()){
                            Bullet[i].move(-2, 2);
                        }
                    }
                    bulletMoveTime[i]--;
                }
            }
        }
        if(timefire<=0&&scrY<=1){
            for(int i=0;i<nBullet;i++){
                if(isShoot[i]==0){
                    Bullet[i].setPosition(iX, iY);
                    Bullet[i].setVisible(true);
                    isShoot[i]=1;
                    bulletMoveTime[i]=30;
                    for(int j=i+1;j<nBullet;j++){
                        if(isShoot[j]==0){
                            Bullet[j].setPosition(iX, iY);
                            Bullet[j].setVisible(true);
                            isShoot[j]=2;
                            bulletMoveTime[j]=30;
                            for(int k=j+1;k<nBullet;k++){
                                if(isShoot[k]==0){
                                    Bullet[k].setPosition(iX, iY);
                                    Bullet[k].setVisible(true);
                                    isShoot[k]=3;
                                    if(health>700||(health<=500&&health>300)){
                                        timefire=40;
                                    }
                                    else if((health<=700&&health>500)||health<=300){
                                        timefire=80;
                                    }
                                    bulletMoveTime[k]=30;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    public int checkFire(Sprite sprite,int health){
        for(int i=0;i<nBullet;i++){
            if(Bullet[i].collidesWith(sprite, true)){
                Bullet[i].setVisible(false);
                isShoot[i]=0;
                health--;
            }
        }
        return health;
    }
    public int getHealth(){
        return health;
    }
    public void setHealth(int health){
        this.health=health;
    }
    public void refreshBoss(){
        health=1000;
        for(int i=0;i<nBullet;i++){
            Bullet[i].setVisible(false);
        }
    }
    public void BossFrame(){
        /*if(timewait<=0){
            boss.nextFrame();
            timewait=4;
        }
        timewait--;*/
    }
}
